package com.effvi.jdk.collection.hashmap;

import java.util.HashMap;
import java.util.Map;
/**
 * HashMap初始化带来的影响
 * @author kelvin.zhang
 *
 */
public class HashMapStudy {
	public static void main(String[] args) {
		HashMapStudy handler = new HashMapStudy();
		handler.test1();//测试hash重构带来的性能影响
	}
	
	private void test1(){
		//默认16容量,装载因子0.75
		Map<String,String> hash = new HashMap<String, String>();
		long s1 = System.currentTimeMillis();
		for(int i = 1 ;i<=100000;i++){
			hash.put(String.valueOf(i), String.valueOf(i));
		}
		long s2 = System.currentTimeMillis();
		System.out.println("默认耗时: "+(s2-s1)+" ms");
		
		//预估容量，给予初始值减少hash重构带来的性能开销
		Map<String,String> hash1 = new HashMap<String, String>(100000,1);
		long s3 = System.currentTimeMillis();
		for(int i = 1 ;i<=100000;i++){
			hash1.put(String.valueOf(i), String.valueOf(i));
		}
		long s4 = System.currentTimeMillis();
		System.out.println("优化后耗时: "+(s4-s3)+" ms");
	}
}
